package Drawable;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import FPGrowth.FPGrowth;

public class MainForm extends JFrame {

	FPGrowth fpgrowth;
	File file;

	public MainForm() {
		super("FP Growth Algorithm");

		// file menu
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');

		// import item
		JMenuItem importItem = new JMenuItem("Import");
		importItem.setMnemonic('I');
		fileMenu.add(importItem);
		importItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// browse file
				JFileChooser chooser = new JFileChooser();
				int choice = chooser.showOpenDialog(MainForm.this);
				if (choice == JFileChooser.APPROVE_OPTION) {
					file = chooser.getSelectedFile();
				}

				chooser.setSelectedFile(null);

			}
		});

		// exit item
		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic('x');
		fileMenu.add(exitItem);
		exitItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});

		// help item
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('H');

		// about item
		JMenuItem aboutItem = new JMenuItem("About");
		aboutItem.setMnemonic('A');
		helpMenu.add(aboutItem);
		aboutItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent event) {
				JOptionPane.showMessageDialog(MainForm.this, "(c) Copyright SP, 2013 version 0.1. All rights reserved", "About",
						JOptionPane.PLAIN_MESSAGE);
			}
		});

		// menu bar
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);
		// tab
		JTabbedPane tab = new JTabbedPane();

		// show association rule
		JLabel label = new JLabel("Choose input file from file menu, please.",
				SwingConstants.CENTER);

		JPanel panel1 = new JPanel(new BorderLayout());
		JPanel panel3 = new JPanel(new BorderLayout());
		JTextArea textArea = new JTextArea();
		
				textArea.setText("Choose input file from file menu, please.");
		//textArea.setVisible(true);
		JScrollPane scroll = new JScrollPane(textArea);
		scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		panel3.add(scroll);
		
		JPanel panel2 = new JPanel();
		panel2.setBackground(Color.GRAY);
		JButton run = new JButton("Run");
		JButton viewfp = new JButton("View FPTree");
		JButton exit = new JButton("Exit");
		panel2.add(run);

		JLabel supportLabel = new JLabel("Input min support");
		JLabel thresholdLabel = new JLabel("Input min threshold");
		JTextField supportTextField = new JTextField(10);
		JTextField thresholdTextField = new JTextField(10);
		JPanel inputPanel = new JPanel(new GridLayout(0, 2));

		inputPanel.add(supportLabel);
		inputPanel.add(supportTextField);
		inputPanel.add(thresholdLabel);
		inputPanel.add(thresholdTextField);

		run.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int minSupport = 0;
				int minThreshold = 0;
				int result;


				if (file != null) {
					while (true) {
						result = JOptionPane.showConfirmDialog(null,
								inputPanel, "Input min support and min threshold",
								JOptionPane.OK_CANCEL_OPTION,
								JOptionPane.PLAIN_MESSAGE);

						if (result == JOptionPane.OK_OPTION) {
							if (supportTextField.getText().equals("")) {
								JOptionPane.showMessageDialog(null,
										"Please enter minimum support");
							} else {
								if (thresholdTextField.getText().equals("")) {
									JOptionPane.showMessageDialog(null,
											"Please enter minimum threshold");
								} else {
									minSupport = Integer.parseInt(supportTextField
											.getText());
									minThreshold = Integer
											.parseInt(thresholdTextField.getText());
									break;
								}
							}
						}
						else {
							break;
						}
					}
					if(result == JOptionPane.OK_OPTION){
					fpgrowth = new FPGrowth(file, minSupport, minThreshold);
					//fpgrowth.showRule();
					try {
						File assocFile = new File("assoc_rule.txt");
						BufferedReader reader = new BufferedReader(new FileReader(assocFile));
						String line = reader.readLine();
						StringBuilder content = new StringBuilder();
						content.append("------Association rules------");
						while(line != null) {
							content.append(line+"\n");
							
							line = reader.readLine();
						}
						textArea.setText(content.toString());
						reader.close();
					} catch (Exception e) {
						
					}
					}
				} else
					JOptionPane.showMessageDialog(null,
							"Please choose file to run!");

			}
		});
		panel2.add(viewfp);
		viewfp.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (fpgrowth != null)
					fpgrowth.showTree();
				else
					JOptionPane.showMessageDialog(null, "Please run first!");
			}
		});
		panel2.add(exit);
		exit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});

		panel1.add(panel2, BorderLayout.SOUTH);
		panel1.add(panel3, BorderLayout.CENTER);
		//panel1.add(label);

		tab.add("Association Rule", panel1);

		this.add(tab);
	}

	public static void main(String[] args) {
		MainForm form = new MainForm();
		form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		form.setMinimumSize(new Dimension(600, 600));
		form.setVisible(true);
	}
}
